<?php
define( '__ESPACE__', 'admin');
$prefix = '../../';
require_once($prefix.'starter.php');

ob_start();

$PWE_admin = new controler_admin();

$propriete = str_replace('modele_', '', $_GET['propriete']);

$modele = null;
$translate = null;
$translatedLink = null;

if(isset($_GET['modele'])&&$_GET['modele']!==''&&isset($_GET['modele_id'])&&($_GET['modele_id']+0)>0){
    //eval('$modele = new modele_'.$_GET['modele'].'('.$_GET['modele_id'].');');

    //if($modele!==null && $modele->getId()>0){
        $translate = modele_translate::findObjectProperty($_GET['modele_id'], $_GET['modele'], $propriete, $_GET['lang']);
        $translatedLink = modele_translate::findObjectProperty($_GET['modele_id'], $_GET['modele'], 'lien', $_GET['lang']);
    //}
}
?>
<!DOCTYPE html>
<html>
    <head>
        <title>Traduction</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="robots" content="noindex,nofollow">
        <link href='http://fonts.googleapis.com/css?family=Grand+Hotel' rel='stylesheet' type='text/css'>
        <link href="../css/style.css" type="text/css" rel="stylesheet" media="all"/>
        <script src="../../js/libs/jquery/jquery.js" type="text/javascript"></script>
        <link href="<?php echo $PWE_admin->getUiThemePath(); ?>" type="text/css" rel="stylesheet" media="all"/>
        <script src="../../js/libs/jqueryui/jquery-ui.js" type="text/javascript"></script>
        <script src="<?php echo $PWE_admin->getBaseURL(); ?>js/libs/ckeditor/ckeditor.js" type="text/javascript"></script>
        <script>
            var baseUrl = "<?php echo $PWE_admin->getBaseURL(); ?>";
            var themePath = "<?php echo $PWE_admin->getThemePath(); ?>";
            <?php if(file_exists($PWE_admin->getThemePath().'/admin/js/styles.js')): ?>
            var ckeditorStylesDeclaration = 'theme:../../<?php echo $PWE_admin->getThemePath(); ?>/admin/js/styles.js';
            <?php else: ?>
            var ckeditorStylesDeclaration = 'default:./styles.js';
            <?php endif; ?>
        </script>
        
    <?php if(isset($_GET['modele'])&&$_GET['modele']!==''&&isset($_GET['modele_id'])&&($_GET['modele_id']+0)>0 && isset($_POST) && isset($_POST['submit'])): ?>
        <?php
            if($translate!==null){ // update
                $translate->setDatas(array(
                    'content'=>$_POST['content']
                ));
                $translate->update();
            }else{ //save
                // verification d'un permalien traduit
                if($propriete!=='lien' && $translatedLink===null){
                    $lien = '';
                    if($modele!==null&&method_exists($modele, 'getPermalien')){
                        $lien = $modele->getPermalien()->getLien();
                    }
                    if($lien!==''){
                        $translatedLink = new modele_translate();
                        $translatedLink->setDatas(array(
                            'modele'=>$_GET['modele'],
                            'modele_id'=>$_GET['modele_id'],
                            'lang'=>$_GET['lang'],
                            'propriete'=>'lien',
                            'content'=>$_GET['lang'].'-'.$lien
                        ));
                        $translatedLink->save();
                    }
                }
                // creation de la ligne traduite
                $translate = new modele_translate();
                $translate->setDatas(array(
                    'modele'=>$_GET['modele'],
                    'modele_id'=>$_GET['modele_id'],
                    'lang'=>$_GET['lang'],
                    'propriete'=>$propriete,
                    'content'=>$_POST["content"]
                ));
                $translate->save();
            }
        ?>
        <script type="text/javascript">
            var origine = window.top;
            origine.closeDialogBox();
        </script>
    </head>
    <body>
        enregistrement effectué
    </body>
    <?php else: ?>
        <script type="text/javascript">
            var origine = window.top;
            
            $(document).ready(function(){
                if(typeof CKEDITOR !== 'undefined'){
                    CKEDITOR.replaceAll( 'editor', {
                        customConfig: './config.js'
                    });
                }
                $('.pwe-ui-btn').button();
                $('form').submit(function(){
                    if( $('#content').val() !== '' ){
                        return true;
                    }else{
                        return false;
                    }
                });
            });
        </script>
    </head>
    <body>
        <form method="post" enctype="multipart/form-data" action="?lang=<?php echo $_GET['lang']; ?>&modele=<?php echo $_GET['modele']; ?>&modele_id=<?php echo $_GET['modele_id']; ?>&propriete=<?php echo $_GET['propriete']; ?>&input=<?php echo $_GET['input']; ?>">
            <strong><?php echo $propriete; ?></strong> : 
            <?php if($_GET['input']==='input'): ?>
            <input name="content" id="content" value="<?php echo ($translate!==null)?$translate->getContent():''; ?>" />
            <?php elseif($_GET['input']==='textarea'): ?>
            <textarea style="width: 760px; height: 380px;" name="content" id="content" <?php echo (isset($_GET['editor'])&&$_GET['editor']==1)?'class="editor"':''; ?>><?php echo ($translate!==null)?$translate->getContent():''; ?></textarea><br/>
            <?php endif; ?>
            <input id="submit"  type="submit" name="submit" value="enregistrer" class="pwe-ui-btn"/>
        </form>
    </body>
    <?php endif; ?>
</html>
<?php ob_end_flush(); ?>